/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testadventure;

import edu.clemson.cs.hamptos.adventure.AdventureCommand;
import edu.clemson.cs.hamptos.adventure.AdventureEngine;
import edu.clemson.cs.hamptos.adventure.AdventureTarget;
import edu.clemson.cs.hamptos.adventure.AdventureWindow;
import edu.clemson.cs.hamptos.adventure.DoNotUnderstandException;
import edu.clemson.cs.hamptos.adventure.VerbStrategyAlt;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;

/**
 *<p>Extends the <code>ItemTool</code> class.</p>
 * @author Nicks
 */
public class ItemFull extends ItemTool {
    /**
     * <p>Constructs a new instance of ItemFull</p>
     */
    protected ItemFull()
    {
        
    }
    
    /**
     * <p>Takes in a text blob from the script file to initialize the class,
     * afterwards will strip all the used fields from the 
     * <code>StringBuilder</code> instance</p>
     * @param inBlob a properly formatted <code>StringBuilder</code> blob 
     * delimited by semicolons, then newlines, and then spaces, it will be 
     * modified by this constructor
     */
    public ItemFull(StringBuilder inBlob)
    {
        super(inBlob);
        
        Scanner blobScan = new Scanner(inBlob.toString());
        blobScan.useDelimiter(";\r\n");
        
        Scanner verbDOScan = new Scanner(blobScan.next());
        verbDOScan.useDelimiter(" ");
        while (verbDOScan.hasNext())
        {
            String text = verbDOScan.next();
            if (text.length() > 0)
            {
                VerbStrategyAlt vsa = TheAdventure.verbMap.get(text);
                if(vsa == null)
                    break;
                Set<String> verbs = vsa.getVerbs();
                VContainer vc = new VContainer(vsa, "y", "n");
                Iterator<String> it = verbs.iterator();
                while(it.hasNext())
                {
                    accDOVerbs.put(it.next(), vc);
                }
            }
        }
        
        Scanner ioScan = new Scanner(blobScan.next());
        ioScan.useDelimiter(" ");
        while (ioScan.hasNext())
        {
            String text = ioScan.next();
            if (text.length() > 0)
            {
                accIOs.add(text);
            }
        }
        
        //removes read fields from inBlob
        inBlob.setLength(0);
        while (blobScan.hasNext())
        {
            inBlob.append(blobScan.next()).append(";\r\n");
        }
    }
    
    

    @Override
    public void doCommandTo(AdventureCommand c, AdventureEngine e, 
                            AdventureWindow w) 
                            throws DoNotUnderstandException {
        //check to see if the verb is acceptable to the do
        String verb = c.getVerb();
        
        if (!(checkVerbAsDO(verb)))
        {
            throw new DoNotUnderstandException(c, "You can't " + c.getVerb() 
                                               + " " + shortDescr);
        }
        
        //see if do can be used
        if (stats.get("enabled") == 0)
        {
            w.println(shortDescr + " can't be used right now.");
            return;
        }
        
        //act
        accDOVerbs.get(verb).getVerbStrategy().doCommand(c, e, w);
        notifyListeners(e, w);
    }
    /**
     * 
     * @param cl 
     */
    protected void fillClone(ItemFull cl)
    {
        super.fillClone(cl);
    }
    
    @Override
    public AdventureTarget clone()
    {
        ItemFull cl = new ItemFull();
        fillClone(cl);
        
        return cl;
    }
    
    @Override
    public void listen(AdventureEngine e, AdventureWindow w)
    {
        if (stats.containsKey("charges"))
        {
            if (stats.get("charges") > 0)
            {
                if (stats.get("enabled") == 0)
                {
                    w.println(activatedTxt);
                    stats.put("enabled", 1);
                }
                else
                {
                    int newval = stats.get("charges") - 1;
                    stats.put("charges", newval);
                    w.println(deactivatedTxt);
                    if(newval == 0)
                    {
                        stats.put("enabled", 0);
                        w.println(disabledTxt);
                    }
                }
            }
        }
        else
        {
            if (stats.get("enabled") == 0)
            {
                stats.put("enabled", 1);
                w.println(activatedTxt);
            }
            else
            {
                stats.put("enabled", 0);
                w.println(deactivatedTxt);
            }
        }
    }
}
